<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */
namespace App\Ordersys\Repositories;


use App\Admin\Models\MemberModel;
use App\Ordersys\Contracts\OrdersysCircleFriendsInterface;
use App\Ordersys\Models\AwardLogModel;
use App\Ordersys\Models\OrdersysCircleFileModel;
use App\Ordersys\Models\OrdersysCircleFriendsModel;
use App\Ordersys\Models\OrdersysCircleMaterialModel;
use App\Ordersys\Models\OrdersysFriendsUserslogModel;
use Illuminate\Support\Facades\Request;


/**
 * 朋友圈
 * Class DbOrdersysCircleFriendsRepository
 * @package App\Ordersys\Repositories
 */
class DbOrdersysCircleFriendsRepository implements OrdersysCircleFriendsInterface
{

    /**
     * @return OrdersysCircleFriendsModel
     */
    public function getOrdersysCircleFriendsModel(){
        return app()->make(OrdersysCircleFriendsModel::class);
    }

    /**
     * @return OrdersysCircleFileModel
     */
    public function getOrdersysCircleFileModel(){
        return app()->make(OrdersysCircleFileModel::class);
    }

    /**
     *
     * @return OrdersysCircleMaterialModel
     */
    public function getOrdersysCircleMaterialModel(){
        return app()->make(OrdersysCircleMaterialModel::class);
    }

    /**
     * 接受数据
     * @return OrdersysFriendsUserslogModel
     */
    public function getOrdersysFriendsUserslogModel(){
        return app()->make(OrdersysFriendsUserslogModel::class);
    }

    /**
     * @return MemberModel
     */

    public function getMemberModel(){
        return app()->make(MemberModel::class);
    }
    /**
     * 获取订单产品列表
     * @param $where
     * @return mixed
     */
    public function getList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysCircleFriendsModel()->newQuery()->select($select)->where($where)->orderBy($sort,$order)->paginate($limit);
    }
    /**
     * 日志
     * @return AwardLogModel
     */
    public function getAwardLogModel(){
        return app()->make(AwardLogModel::class);
    }

    /**
     * 保存公告数据
     * @param $data
     * @return mixed
     */
    public function saveData($data){
        if(empty($data['status'])){
            $data['status']=0;
        }
        $data["uid"]=UID;
        //当数目时候
         $this->getMemberModel()->newQuery()->where("uid",UID)->increment("aritcle_num",1);


        $aritleInfo=$this->getOrdersysCircleFriendsModel()->newQuery()->updateOrCreate(["id"=>$data['id']],$data);

        $adata=array();
        $adata["award_type"]=2;
        $adata["uid"]=UID;
        $adata["order_id"]=$aritleInfo->id;
        $adata['money']= 1;
        $adata["remark"]="发布朋友圈：".$aritleInfo->id.",超过设定超过设定数量:";
        $this->getAwardLogModel()->awardLog(2,$adata);
        return $aritleInfo;
    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed
     */
    public function getInfo($id){
        return $this->getOrdersysCircleFriendsModel()->newQuery()->where("id",$id)->first();
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($where){
        $this->getMemberModel()->newQuery()->where("uid",UID)->decrement("aritcle_num",1);
        return $this->getOrdersysCircleFriendsModel()->newQuery()->where($where)->delete();
    }


    public function acceptData($data){
		$inData=$data;
		unset($inData['id']);
      if($this->getOrdersysFriendsUserslogModel()->newQuery()->updateOrCreate(["friends_id"=>$data['id'],"uid"=>UID],$inData)) {
          $this->getOrdersysCircleFriendsModel()->newQuery()->where("id",$data['id'])->increment("show_num");
      }
    }

    /**
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getCircleFileList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysCircleFileModel()->newQuery()->select($select)->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    /**
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getCircleMaterialList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'create_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysCircleMaterialModel()->newQuery()->select($select)->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    /**
     * @param $data
     */
    public  function saveCircleMaterialData($data){
        if(empty($data['status'])){
            $data['status']=0;
        }
        $data["uid"]=UID;
        return $this->getOrdersysCircleMaterialModel()->newQuery()->updateOrCreate(["id"=>$data['id']],$data);
    }

    public  function saveCircleFileData($data){
        if(empty($data['status'])){
            $data['status']=0;
        }
        $data["uid"]=UID;
        return $this->getOrdersysCircleFileModel()->newQuery()->updateOrCreate(["id"=>$data['id']],$data);
    }

    public function deleteCircleMaterialData($where){
        return $this->getOrdersysCircleMaterialModel()->newQuery()->where($where)->delete();
    }
    public function deleteCircleFileData($where){
        return $this->getOrdersysCircleFileModel()->newQuery()->where($where)->delete();
    }

    public function getCircleMaterialInfo($id){
        return $this->getOrdersysCircleMaterialModel()->newQuery()->where("id",$id)->first();
    }
    public function getCircleFileInfo($id){
        return $this->getOrdersysCircleFileModel()->newQuery()->where("id",$id)->first();
    }
}
